﻿using System;
using System.Runtime.InteropServices;

/* Code taken from
 * http://stackoverflow.com/questions/2416748/how-to-simulate-mouse-click-in-c
 */
namespace Handsfree
{
    public class MouseOperations
    {
        [Flags]
        public enum MouseEventFlags
        {
            LeftDown = 0x00000002,
            LeftUp = 0x00000004,
            MiddleDown = 0x00000020,
            MiddleUp = 0x00000040,
            Move = 0x00000001,
            Absolute = 0x00008000,
            RightDown = 0x00000008,
            RightUp = 0x00000010
        }

        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetCursorPos(out MousePoint lpMousePoint);

        [DllImport("user32.dll")]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        public static void SetCursorPosition(int X, int Y)
        {
            SetCursorPos(X, Y);
        }

        public static void MoveCursorPosition(int dx, int dy)
        {
            MousePoint current = GetCursorPosition();
            SetCursorPosition(current.X + dx, current.Y + dy);
        }
        public static MousePoint GetCursorPosition()
        {
            MousePoint currentMousePoint;
            var gotPoint = GetCursorPos(out currentMousePoint);
            if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); }
            return currentMousePoint;
        }

        public static void MouseEvent(MouseEventFlags value)
        {
            MousePoint position = GetCursorPosition();

            mouse_event
                ((int)value,
                 position.X,
                 position.Y,
                 0,
                 0)
                ;
        }
        public void Click(int n)
        {
            for (int i = 1; i <= n; i++)
            {
                MouseEvent(MouseEventFlags.LeftDown);
                MouseEvent(MouseEventFlags.LeftUp);
            }
        }
        public void RightClick(int n)
        {
            for (int i = 1; i <= n; i++)
            {
                MouseEvent(MouseEventFlags.RightDown);
                MouseEvent(MouseEventFlags.RightUp);
            }
        }
        public void MiddleClick(int n)
        {
            for (int i = 1; i <= n; i++)
            {
                MouseEvent(MouseEventFlags.MiddleDown);
                MouseEvent(MouseEventFlags.MiddleUp);
            }
        }
        public void MoveMouse(int dx, int dy)
        {
            MoveCursorPosition(dx, dy);
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct MousePoint
        {
            public int X;
            public int Y;

            public MousePoint(int x, int y)
            {
                X = x;
                Y = x;
            }

        }
    }
}